package com.sportmall.controller.portal;

import com.github.pagehelper.PageInfo;
import com.sportmall.common.Const;
import com.sportmall.common.ResponseCode;
import com.sportmall.common.ServerResponse;
import com.sportmall.pojo.User;
import com.sportmall.service.IUserService;
import com.sportmall.vo.UserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user/")
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private IUserService iUserService;

    /**
     * 用户登录
     * @param session
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "login.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse login(HttpSession session, String username, String password){

        ServerResponse<UserVo> response = iUserService.userLogin(username, password);
        if (response.isSucces()){
            UserVo userVo = response.getData();
            session.setAttribute(Const.CURRENT_USER, userVo);
        }

        return response;
    }

    /**
     * 用户注册
     * @param session
     * @param user
     * @return
     */
    @RequestMapping(value = "register.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse register(HttpSession session, User user){
        return iUserService.userRegister(user);
    }

    /**
     * 用户注销
     * @param session
     * @return
     */
    @RequestMapping(value = "logout.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse logout(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user != null){
            session.removeAttribute(Const.CURRENT_USER);
            return ServerResponse.createBySuccessMessage("退出成功");
        }
        return ServerResponse.createByErrorMessage("用户未登录");
    }

    /**
     * 登录状态获取用户信息
     * @param session
     * @return
     */
    @RequestMapping(value = "get_user_info.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getUserInfo(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user != null){
            return iUserService.selectUserInfo(user.getId());
        }
        return ServerResponse.createByErrorCodeMassage(ResponseCode.NEED_LOGIN, ResponseCode.NEED_LOGIN.getDesc());
    }

    /**
     * 登录状态更新用户信息
     * @param session
     * @param requestUser
     * @return
     */
    @RequestMapping(value = "update_user.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse updateUser(HttpSession session, User requestUser){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user != null){
            return iUserService.updateUser(user.getId(), requestUser);
        }
        return ServerResponse.createByErrorCodeMassage(ResponseCode.NEED_LOGIN, ResponseCode.NEED_LOGIN.getDesc());
    }

    /**
     * 登录状态修改密码
     * @param session
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @RequestMapping(value = "reset_password.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse resetPassword(HttpSession session, String oldPassword, String newPassword){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user != null){
            return iUserService.resetPassword(user.getId(), oldPassword, newPassword);
        }
        return ServerResponse.createByErrorCodeMassage(ResponseCode.NEED_LOGIN, ResponseCode.NEED_LOGIN.getDesc());
    }

    /**
     * 找回密码-检查用户名是否有效
     * @param session
     * @param username
     * @return
     */
    @RequestMapping(value = "check_user_valid.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse checkUserValid(HttpSession session, String username){
        return iUserService.checkUserValid(username);
    }

    /**
     * 找回密码-忘记密码
     * 查找用户设定的问题
     * @param session
     * @param username
     * @return
     */
    @RequestMapping(value = "forget_get_question.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse forgetGetQuestion(HttpSession session, String username){
        return iUserService.forgetSelectQuestion(username);
    }

    /**
     * 找回密码-验证回答问题
     * @param session
     * @param username
     * @param question
     * @param answer
     * @return
     */
    @RequestMapping(value = "forget_check_answer.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse forgetCheckAnswer(HttpSession session, String username, String question, String answer){
        return iUserService.forgetCheckAnswer(username, question, answer);
    }

    /**
     * 找回密码-修改密码
     * @param session
     * @param username
     * @param forgetToken
     * @param newPassword
     * @return
     */
    @RequestMapping(value = "forget_reset_password.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse forgetResetPassword(HttpSession session, String username, String forgetToken, String newPassword){
        return iUserService.forgetResetPassword(username, forgetToken, newPassword);
    }
}
